//
//  TravelsPhotoDetailsView.m
//  艾听歌
//
//  Created by dllo on 16/5/29.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "TravelsPhotoDetailsView.h"

#define ViewW self.frame.size.width
#define ViewH self.frame.size.height

@interface TravelsPhotoDetailsView ()<UIScrollViewDelegate>


@property (nonatomic, strong) UIScrollView *smallView;

@end

@implementation TravelsPhotoDetailsView


- (instancetype)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    if (self) {
        
        [self createSubViews];
    }
    
    return self;
}


- (void)createSubViews{
    
    self.smallView = [[UIScrollView alloc]initWithFrame:self.bounds];
    self.smallView.backgroundColor = [UIColor blackColor];
    [self addSubview:self.smallView];
    
    //签协议,获取缩放方法
    self.smallView.delegate = self;
    self.smallView.minimumZoomScale = 0.5;
    self.smallView.maximumZoomScale = 2.0;
    
        
    self.photoImageView = [[UIImageView alloc]init];
    self.photoImageView.backgroundColor = [UIColor yellowColor];
    [self.smallView addSubview:self.photoImageView];
    self.photoImageView.userInteractionEnabled = YES;
    
    
    self.descriptionLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, ViewH - 100, ViewW, 100)];
    self.descriptionLabel.backgroundColor = [UIColor blackColor];
    [self addSubview:self.descriptionLabel];
    self.descriptionLabel.userInteractionEnabled = YES;
    
    
    //添加轻拍方法
    UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction)];
    UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction)];
    UITapGestureRecognizer *tap3 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction)];
    
    [self.smallView addGestureRecognizer:tap1];
    [self.photoImageView addGestureRecognizer:tap2];
    [self.descriptionLabel addGestureRecognizer:tap3];
    
    
}


- (void)layoutSubviews{
    
    CGFloat newHeight = ViewW * [self.photoDetailModel.height floatValue] / [self.photoDetailModel.width floatValue];
    if (newHeight > ViewH - 100) {
        newHeight = ViewH - 100;
    }
    
    self.photoImageView.frame = CGRectMake(0, (self.smallView.frame.size.height - newHeight) / 2 + 50 , ViewW, newHeight - 100);
    
    
}

- (void)setPhotoDetailModel:(ModuleFirst_UserLoveModel *)photoDetailModel{
    
    _photoDetailModel = photoDetailModel;
    
    [self.photoImageView sd_setImageWithURL:[NSURL URLWithString:_photoDetailModel.photo_url] placeholderImage:[UIImage imageNamed:@"holderr"]];
    
    self.descriptionLabel.text = _photoDetailModel.description1;
    self.descriptionLabel.textColor = [UIColor whiteColor];
    self.descriptionLabel.font = [UIFont systemFontOfSize:17];
    self.descriptionLabel.numberOfLines = 0;
    [self.descriptionLabel sizeToFit];
    
}


//缩放
//1.允许缩放
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
    
    return [scrollView.subviews firstObject];
}

//2.缩放过程中
-(void)scrollViewDidZoom:(UIScrollView *)scrollView{
    //缩放过程中让照片的中心点位于屏幕的中间,避免跳屏
    UIImageView *isImageView = [scrollView.subviews firstObject];
    isImageView.center = CGPointMake(scrollView.frame.size.width / 2, scrollView.frame.size.height / 2);
    
}

//3.缩放结束
-(void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale{
    //缩放结束让照片的中心点位于屏幕的中间
    
    if ([scrollView isKindOfClass:[UIScrollView class]]) {
        scrollView.zoomScale = 1.0;
        //滑动结束后各个子Scroll中的照片尺寸恢复满屏大小
        UIImageView *isImageView = [scrollView.subviews firstObject];
        CGFloat newHeight = ViewW * [self.photoDetailModel.height floatValue] / [self.photoDetailModel.width floatValue];
        if (newHeight > self.smallView.frame.size.height - 100) {
            newHeight = self.smallView.frame.size.height - 100;
        }
        
        isImageView.frame = CGRectMake(0, (self.smallView.frame.size.height - newHeight) / 2 + 50 , ViewW, newHeight - 100);
    }
    
}


//轻拍手势方法的实现

- (void)tapAction{
    
    self.backToUserHomepage();
    
    
}




@end
